﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 捡起的物品
/// </summary>
public class PickUpArticle : ShowHideBehaviour {
    
    private Image PickUpImage;
    /// <summary>
    /// 拿起的物品数量
    /// </summary>
    //public int Amount { get; private set; }
    private Text AmountText;
    //public Article Article { get; private set; }
    private ArticleInfo _articleInfo = new ArticleInfo();
    public ArticleInfo ArticleInfo {
        get {
            return this._articleInfo;
        }
    }
    /// <summary>
    /// 拿起物品的格子
    /// </summary>
    public ArticleItem ArticleItem { get; private set; }
    /// <summary>
    /// 是否已经捡起
    /// </summary>
    public bool IsPickUp { get; private set; }
    private float Smoothing = 10;

	// Use this for initialization
	void Start () {
        base.InitShowHide();
        this.PickUpImage = GetComponent<Image>();
        this.AmountText = this.transform.Find("Amount").GetComponent<Text>();
	}
	
	// Update is called once per frame
	void Update () {
        base.HandleShowHide(this.Smoothing);
    }

    /// <summary>
    /// 捡起物品
    /// </summary>
    /// <param name="articleItem"></param>
    /// <param name="amount">拾取数量</param>
    public void PickUp(ArticleItem articleItem, int amount) {
        this.ArticleItem = articleItem;
        this.ArticleInfo.Amount = amount;
        this.ArticleInfo.Article = articleItem.ArticleInfo.Article;
        this.PickUpImage.sprite = Resources.Load<Sprite>(this.ArticleInfo.Article.ArticleTemplet.Sprite);
        this.ShowText();
        this.IsPickUp = true;
        base.Show();
    }

    public void PutDownAll() {
        this.ArticleItem.UnPickUpAll();
        this.ArticleItem = null;
        this.ArticleInfo.Article = null;
        this.ArticleInfo.Amount = 0;
        this.IsPickUp = false;
        base.Hide();
    }

    public int PutDown(int amount = 1) {
        if (amount > this.ArticleInfo.Amount) { // 如果放下数量大于拿起数量，则按拿起数量放下
            amount = this.ArticleInfo.Amount;
        }
        if (amount == this.ArticleInfo.Amount) { // 全部放下
            this.PutDownAll();
        } else { // 放下部分
            this.ArticleInfo.Amount -= amount;
            this.ShowText();
        }
        return amount;
    }

    /// <summary>
    /// 跟随鼠标移动
    /// </summary>
    /// <param name="canvas"></param>
    public void FollowMouse(Canvas canvas) {
        if (this.IsPickUp) {
            UIHelper.FollowMouse(canvas, this);
        }
    }

    private void ShowText() {
        this.AmountText.text = this.ArticleInfo.Article.ArticleTemplet.SpaceNum != 1 ? this.ArticleInfo.Amount.ToString() : ""; // 容量不等于1时才显示数量
    }
}
